<?php
/**
 * 基于数组实现的队列
 */

namespace Xxb\XxbPhpAlgo;

use Xxb\XxbPhpAlgo\Contract\XxbQueueInterface;

class ArrayQueue implements XxbQueueInterface
{
    /**
     * @var FixedArray
     */
    private $queue;

    public function __construct($capacity = 10)
    {
        $this->queue = new FixedArray($capacity);
    }

    public function push($value)
    {
        $this->queue->addLast($value);
    }

    public function shift()
    {
        return $this->queue->removeFirst();
    }

    public function bottom()
    {
        return $this->queue->getFirst();
    }

    public function top()
    {
        return $this->queue->getLast();
    }

    public function isEmpty()
    {
        return $this->queue->isEmpty();
    }

    public function getSize()
    {
        return $this->queue->getSize();
    }

    public function __toString()
    {
        $str = 'queue: top [';
        for ($i=0; $i<$this->getSize(); $i++) {
            $str .= $this->queue->get($i);
            $str .= ',';
        }
        $str = rtrim($str, ',');
        $str .= ']';
        return $str;
    }
}
